Applications have many tasks that need to be performed at regular intervals, such as checking whether a user has requested the cancellation of an operation. A typical solution is to start a helper thread that sleeps most of the time, but wakes up at regular intervals, checks whether a task is pending, executes the task, and then returns to sleep. However, this solution requires considerable operating system and central processing unit (CPU) resources because a thread needs to be created and regularly scheduled by the operating system. When starting a separate thread is not possible or feasible (e.g., due to single-threaded execution resulting from hardware or software restrictions), checks are often introduced directly into the application code, adversely affecting maintainability and reliability.
Virtual machines (VMs) use the “safepoint” (sometimes also called “yieldpoint”) mechanism to interrupt application code when VM code (e.g., garbage collection) needs to run. One way to implement safepoints is to check a thread-local flag at every method return and/or loop iteration. However, this implementation does not help with tasks that need to be performed at regular intervals, since a thread stops instantly (i.e., not periodically) when a safepoint is requested, and a safepoint needs to be requested from a different thread.